Add scenario_label view helper to render scenario tags.

Guilherme J. Tramontina 9 years ago
parent
commit
5921845126

+ 15 - 2
app/helpers/scenario_helper.rb

@@ -2,9 +2,22 @@ module ScenarioHelper
2 2
 
3 3
   def style_colors(scenario)
4 4
     colors = {
5
-      color: scenario.tag_fg_color,
6
-      background_color: scenario.tag_bg_color
5
+      color: scenario.tag_fg_color || default_scenario_fg_color,
6
+      background_color: scenario.tag_bg_color || default_scenario_bg_color
7 7
     }.map { |key, value| "#{key.to_s.dasherize}:#{value}" }.join(';')
8 8
   end
9 9
 
10
+  def scenario_label(scenario, text = nil)
11
+    text ||= scenario.name
12
+    content_tag :span, text, class: 'label scenario', style: style_colors(scenario)
13
+  end
14
+
15
+  def default_scenario_bg_color
16
+    '#5BC0DE'
17
+  end
18
+
19
+  def default_scenario_fg_color
20
+    '#FFFFFF'
21
+  end
22
+
10 23
 end

+ 1 - 1
app/views/agents/_action_menu.html.erb

@@ -32,7 +32,7 @@
32 32
 
33 33
     <% agent.scenarios.each do |scenario| %>
34 34
       <li>
35
-        <%= link_to "<span class='color-warning glyphicon glyphicon-remove-circle'></span> Remove from <span class='scenario label' style='#{style_colors(scenario)}'>#{h scenario.name}</span>".html_safe, leave_scenario_agent_path(agent, :scenario_id => scenario.to_param, :return => returnTo), method: :put, :tabindex => "-1" %>
35
+        <%= link_to "<span class='color-warning glyphicon glyphicon-remove-circle'></span> Remove from #{scenario_label(scenario)}".html_safe, leave_scenario_agent_path(agent, :scenario_id => scenario.to_param, :return => returnTo), method: :put, :tabindex => "-1" %>
36 36
       </li>
37 37
     <% end %>
38 38
   <% end %>

+ 2 - 3
app/views/scenario_imports/_step_two.html.erb

@@ -13,9 +13,8 @@
13 13
       <div class="alert alert-warning">
14 14
         <span class='glyphicon glyphicon-warning-sign'></span>
15 15
         This Scenario already exists in your system. The import will update your existing
16
-        <span class='label scenario' style="<%= style_colors(@scenario_import.existing_scenario) %>"><%= @scenario_import.existing_scenario.name %></span> Scenario's title
17
-        and
18
-        description. Below you can customize how the individual agents get updated.
16
+        <%= scenario_label(@scenario_import.existing_scenario) %> Scenario's title,
17
+        description and tag colors. Below you can customize how the individual agents get updated.
19 18
       </div>
20 19
     <% end %>
21 20
 

+ 2 - 2
app/views/scenarios/_form.html.erb

@@ -18,13 +18,13 @@
18 18
     <div class="col-md-2">
19 19
       <div class="form-group">
20 20
         <%= f.label :tag_bg_color, "Tag Background Color" %>
21
-        <%= f.color_field :tag_bg_color, :class => 'form-control', :placeholder => "#FFFFFF" %>
21
+        <%= f.color_field :tag_bg_color, :class => 'form-control', :value => @scenario.tag_bg_color || default_scenario_bg_color %>
22 22
       </div>
23 23
     </div>
24 24
     <div class="col-md-2">
25 25
       <div class="form-group">
26 26
         <%= f.label :tag_fg_color, "Tag Foreground Color" %>
27
-        <%= f.color_field :tag_fg_color, :class => 'form-control', :placeholder => "#000000" %>
27
+        <%= f.color_field :tag_fg_color, :class => 'form-control', :value => @scenario.tag_fg_color || default_scenario_fg_color %>
28 28
       </div>
29 29
     </div>
30 30
   </div>

+ 1 - 1
app/views/scenarios/index.html.erb

@@ -21,7 +21,7 @@
21 21
         <% @scenarios.each do |scenario| %>
22 22
           <tr>
23 23
             <td>
24
-              <span class="label" style="<%= style_colors(scenario) %>" title="Tag"><i class="glyphicon glyphicon-font"></i></span>
24
+              <%= scenario_label(scenario, content_tag(:i, '', class: 'glyphicon glyphicon-font')) %>
25 25
               <%= link_to(scenario.name, scenario) %>
26 26
             </td>
27 27
             <td><%= link_to pluralize(scenario.agents.count, "agent"), scenario %></td>

+ 1 - 1
app/views/scenarios/share.html.erb

@@ -2,7 +2,7 @@
2 2
   <div class='row'>
3 3
     <div class='col-md-12'>
4 4
       <div class="page-header">
5
-        <h2>Share <span class='label scenario' style="<%= style_colors(@scenario) %>"><%= @scenario.name %></span> with the world</h2>
5
+        <h2>Share <%= scenario_label(@scenario) %> with the world</h2>
6 6
       </div>
7 7
 
8 8
       <p>

+ 2 - 1
app/views/scenarios/show.html.erb

@@ -2,7 +2,8 @@
2 2
   <div class='row'>
3 3
     <div class='col-md-12'>
4 4
       <div class="page-header">
5
-        <h2><span class='label scenario' style="<%= style_colors(@scenario) %>"><%= @scenario.name %></span> <%= "Public" if @scenario.public? %> Scenario</h2>
5
+        <h2><%= scenario_label(@scenario) %> <%= "Public" if @scenario.public? %> Scenario</h2>
6
+
6 7
       </div>
7 8
 
8 9
       <% if @scenario.description.present? %>

+ 20 - 2
spec/helpers/scenario_helper_spec.rb

@@ -1,11 +1,29 @@
1 1
 require 'spec_helper'
2 2
 
3 3
 describe ScenarioHelper do
4
+  let(:scenario) { users(:bob).scenarios.build(name: 'Scene', tag_fg_color: '#AAAAAA', tag_bg_color: '#000000') }
4 5
 
5 6
   describe '#style_colors' do
6 7
     it 'returns a css style-formated version of the scenario foreground and background colors' do
7
-      scenario = users(:bob).scenarios.build(tag_fg_color: '#ffffff', tag_bg_color: '#000000')
8
-      style_colors(scenario).should == "color:#ffffff;background-color:#000000"
8
+      style_colors(scenario).should == "color:#AAAAAA;background-color:#000000"
9
+    end
10
+
11
+    it 'defauls foreground and background colors' do
12
+      scenario.tag_fg_color = nil
13
+      scenario.tag_bg_color = nil
14
+      style_colors(scenario).should == "color:#FFFFFF;background-color:#5BC0DE"
15
+    end
16
+  end
17
+
18
+  describe '#scenario_label' do
19
+    it 'creates a scenario label with the scenario name' do
20
+      scenario_label(scenario).should ==
21
+        '<span class="label scenario" style="color:#AAAAAA;background-color:#000000">Scene</span>'
22
+    end
23
+
24
+    it 'creates a scenario label with the given text' do
25
+      scenario_label(scenario, 'Other').should ==
26
+        '<span class="label scenario" style="color:#AAAAAA;background-color:#000000">Other</span>'
9 27
     end
10 28
   end
11 29